// pages/news/news.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
     openId:'',
     ReceiveOrderArray:[],
     active:0,
    },
    onLoad: function (options) {
      
      const openId = wx.getStorageSync('openid');
      this.setData({
          openId
      })
      this.getReceiveOrderList()
    
  },
  onShow:function(){
    this.onLoad()
  },

  // 确认交易
  confirmExchange(e){
    // console.log(e)
    const goodsId = e.target.dataset.goodsid
    console.log(goodsId)
     wx.request({
         url:'http://127.0.0.1:6060/api/confirmExchange',
         method:'get',
         data:{
            goodsId
         },
         success:(res)=>{
           wx.showToast({
             title: '确认成功',
             icon: 'success',
             duration: 1500
           })
           
             this.onLoad()
         }
         
     })
  },

  // 下架发布
  cancelPublish(e){
    console.log(e.target.dataset)
    const goodsId = e.target.dataset.goodsid
    const orderId = e.target.dataset.orderid
    // const goodsId = e.target.data.goodsid
      wx.showModal({
         
          content: '是否删除发布？',
          showCancel: true,
          cancelText: '取消',
          cancelColor: 'grey',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {
              if (result.confirm) {
                  wx.request({
                      url:'http://127.0.0.1:6060/api/cancelPublish',
                      data:{
                          orderId,
                          goodsId
                      },
                      method:'get',
                      
                      success:(res) =>{
                          // console.log(res)
                          // this.onLoad()
                          this.onShow()
                          setTimeout(() => {
                            
                            wx.showToast({
                              title: '删除成功',
                              icon: 'success',
                              duration: 1000
                            })
                          }, 100);
                            
                        
                      }
                  })
              }
          },
          fail: () => {},
          complete: () => {}
      });
        
  //    wx.request({
  //        url:'http://127.0.0.1:6060/api/deleteOneOrder',
  //        data:{
  //            orderId:'bTcx5KoLpXN68KlKZnmv1'
  //        },
  //        method:'get',
  //        success:(res) =>{
  //            console.log(res)
             
  //        }
  //    })
  },

  // 取消交易
  cancelExchange(e){
    // console.log(e)
    const orderId = e.target.dataset.orderid
    const goodsId = e.target.dataset.goodsid
    console.log(goodsId,orderId)
    wx.showModal({
           
      content: '确定取消？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: 'grey',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (result) => {
          if (result.confirm) {
            wx.request({
              url:'http://127.0.0.1:6060/api/cancelExchange',
              method:'get',
              data:{
                 goodsId,
                 orderId
              },
              success:(res)=>{
                wx.showToast({
                  title: '取消成功',
                  icon: 'success',
                  duration: 1500
                })
                
                  this.onLoad()
              }
              
          })
          }
      },
      fail: () => {},
      complete: () => {}
  });
    //  wx.request({
    //      url:'http://127.0.0.1:6060/api/cancelExchange',
    //      method:'get',
    //      data:{
    //         goodsId,
    //         orderId
    //      },
    //      success:(res)=>{
    //        wx.showToast({
    //          title: '取消成功',
    //          icon: 'success',
    //          duration: 1500
    //        })
           
    //          this.onLoad()
    //      }
         
    //  })
  },
  login(e){
    wx.getUserProfile({
        desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
            console.log(res.userInfo)
          this.setData({
            userInfo: res.userInfo,
            
          })
          wx.login({
            //获取code
           
            success:  (res)=> {
              var code = res.code; //返回code
              const userInfo = this.data.userInfo
              wx.setStorageSync("userInfo",userInfo);
              wx.request({
                  url: 'http://127.0.0.1:6060/api/addUserInfo',
                  data: {
                      code,
                      userInfo
                    
                  },
                  header: {'content-type':'application/json'},
                  method: 'post',
                 
                  success: (result) => {
                     
                      console.log(result.data.message)
                      this.setData({
                          openid:result.data.message
                      })
                      wx.setStorageSync("openid",result.data.message);
                      this.onLoad()
                  },
                 
              });
                
            }
        })
          wx.showToast({
              title: '登录成功',
              icon: 'success',
             
              duration: 1500,
              mask: false,
              success: (result) => {
                  
              },
              fail: () => {},
              complete: () => {}
          });
            
       
        }
      })
    },
    
    getReceiveOrderList(){
      wx.showLoading({
        title: '正在加载',
        mask:true
      });
        wx.request({
            url:'http://127.0.0.1:6060/api/getReceiveOrderList',
            method:'get',
            data:{
                userId:this.data.openId
            },
            success:(res)=>{
              this.setData({
                loading:1
              })
              wx.hideLoading()
                console.log(res)
                this.setData({
                    ReceiveOrderArray:res.data.message
                })
            },
            fail: err => {
              wx.hideLoading()
              wx.showToast({
                title: '加载失败',
                icon: 'none'
              })
              console.log(err)
            }
        })
    },
})